import { observable, toJS, action } from 'mobx'
import { post } from '../utils/http'

export default class AppState {
  @observable user = {
    isLogin: false,
    info: {},
  }

  @action login(accessToken) {
    return new Promise((resolve, reject) => {
      post('/user/login', {
        accessToken,
      })
        .then(res => {
          if (res.success) {
            this.user.isLogin = true
            this.user.info = res.data
            resolve()
          } else {
            reject(res)
          }
        })
        .catch(reject)
    })
  }

  init() {}

  toJson() {
    return {
      user: toJS(this.user),
    }
  }
}
